package cn.com.bluemoon.daps.sync.controller;

import cn.com.bluemoon.common.KeyAnnotationClz;
import cn.com.bluemoon.common.UserPermission;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import cn.com.bluemoon.daps.common.enums.SyncToEnv;
import cn.com.bluemoon.daps.sync.entity.DapSyncProdEnvTask;
import cn.com.bluemoon.daps.sync.service.DapModel2ProdSyncService;
import cn.com.bluemoon.daps.sync.vo.model2prod.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Jarod.Kong
 * @date 2021/6/16 16:57
 */
@Api(tags = "发布同步")
@RestController
@Slf4j
@Validated
@RequestMapping("/model2prod")
public class DapModel2ProdSyncController {
    @Resource
    private DapModel2ProdSyncService model2ProdSyncService;

    @BmPermission(value = {"model:table:authorize"}, name = "申请发布通过后的单号列表")
    @PostMapping
    @ApiOperation(value = "发布同步-列表")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", responseContainer = "List", response = SyncModelOrderOut.class)})
    public ResultBean<IPage<SyncModelOrderOut>> syncList(@RequestBody @ApiParam(value = "发布同步模型表分页查询条件") @Valid SyncModelOrderPageVo<DapSyncProdEnvTask> searchVo) {
        IPage<SyncModelOrderOut> res = model2ProdSyncService.querySyncList(searchVo);
        return ResultBean.page(res);
    }


    @BmPermission(value = {"model:table:authorize"}, name = "获取模型单号同步操作记录")
    @GetMapping("/operation")
    @ApiOperation(value = "发布同步-操作记录")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", responseContainer = "List", response = SyncOperRecordOut.class)})
    public ResultBean<List<SyncOperRecordOut>> viewOpers(@RequestParam(value = "modelId") @ApiParam(value = "模型id") String modelId,
                                                         @RequestParam(value = "taskId") @ApiParam(value = "任务id") String taskId) {
        List<SyncOperRecordOut> res = model2ProdSyncService.findOperRecords(modelId, taskId);
        return ResultBean.ok(res);
    }


    @BmPermission(value = {"model:table:authorize"}, name = "获取发布同步详情")
    @GetMapping("/preProd/view")
    @ApiOperation(value = "发布同步-同步预发布")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", response = SyncPublishInfoOut.class)})
    public ResultBean<SyncPublishInfoOut> viewPreProdInfo(@RequestParam(value = "modelId") @ApiParam(value = "模型id") String modelId,
                                                          @RequestParam(value = "taskId") @ApiParam(value = "任务id") String taskId) {
        SyncPublishInfoOut res = model2ProdSyncService.getPublishInfo(modelId, taskId, SyncToEnv.PRE_PROD);
        return ResultBean.ok(res);
    }

    @BmPermission(value = {"model:table:authorize"}, name = "获取发布同步详情")
    @GetMapping("/prod/view")
    @ApiOperation(value = "发布同步-同步生产")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", response = SyncPublishInfoOut.class)})
    public ResultBean<SyncPublishInfoOut> viewProdInfo(@RequestParam(value = "modelId") @ApiParam(value = "模型id") String modelId,
                                                       @RequestParam(value = "taskId") @ApiParam(value = "任务id") String taskId) {
        SyncPublishInfoOut res = model2ProdSyncService.getPublishInfo(modelId, taskId, SyncToEnv.PROD);
        return ResultBean.ok(res);
    }


    @BmPermission(value = {"model:table:authorize"}, name = "发布同步-同步预发布-执行")
    @PostMapping("/preProd/sync_v1")
    @ApiOperation(value = "发布同步-同步预发布-执行")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", response = String.class)})
    @ApiIgnore
    public ResultBean<String> syncPreProd_(@RequestParam(value = "modelId") @ApiParam(value = "模型id") String modelId,
                                           @RequestParam(value = "taskId") @ApiParam(value = "发布同步列表行id") String taskId) {
        boolean res = model2ProdSyncService.syncPublish(SyncToEnv.PRE_PROD, modelId, taskId, UserInfoHolder.getUserInfoOrEmpty());
        return res ? ResultBean.ok() : ResultBean.error("同步失败");
    }

    @BmPermission(value = {"model:table:authorize"}, name = "发布同步-同步预发布-执行")
    @PostMapping("/preProd/sync")
    @ApiOperation(value = "发布同步-同步预发布-执行")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", response = String.class)})
    @UserPermission.SingleRow(key = "modelId", keyFormClz = KeyAnnotationClz.RequestBody)
    public ResultBean<String> syncPreProd(@Valid @RequestBody SyncModelExecParamVo paramVo) {
        boolean res = model2ProdSyncService.syncPublish(SyncToEnv.PRE_PROD, paramVo.getModelId(), paramVo.getTaskId(), UserInfoHolder.getUserInfoOrEmpty());
        return res ? ResultBean.ok() : ResultBean.error("同步失败");
    }

    @BmPermission(value = {"model:table:authorize"}, name = "发布同步-同步生产-执行")
    @PostMapping("/prod/sync_v1")
    @ApiOperation(value = "发布同步-同步生产-执行")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", response = String.class)})
    @ApiIgnore
    public ResultBean<String> syncProd_(@RequestParam(value = "modelId") @ApiParam(value = "模型id") String modelId,
                                        @RequestParam(value = "taskId") @ApiParam(value = "发布同步列表行id") String taskId) {
        boolean res = model2ProdSyncService.syncPublish(SyncToEnv.PROD, modelId, taskId, UserInfoHolder.getUserInfoOrEmpty());
        return res ? ResultBean.ok() : ResultBean.error("同步失败");
    }

    @BmPermission(value = {"model:table:authorize"}, name = "发布同步-同步生产-执行")
    @PostMapping("/prod/sync")
    @ApiOperation(value = "发布同步-同步生产-执行")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", response = String.class)})
    @UserPermission.SingleRow(key = "modelId", keyFormClz = KeyAnnotationClz.RequestBody)
    public ResultBean<String> syncProd(@Valid @RequestBody SyncModelExecParamVo paramVo) {
        boolean res = model2ProdSyncService.syncPublish(SyncToEnv.PROD, paramVo.getModelId(), paramVo.getTaskId(), UserInfoHolder.getUserInfoOrEmpty());
        return res ? ResultBean.ok() : ResultBean.error("同步失败");
    }

    @BmPermission(value = {"model:table:authorize"}, name = "发布同步-同步预发布-查看日志")
    @GetMapping("/preProd/log")
    @ApiOperation(value = "发布同步-同步预发布-查看日志")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", responseContainer = "List", response = String.class)})
    public ResultBean<List<String>> syncPreProdViewLog(@RequestParam(value = "modelId") @ApiParam(value = "模型id") String modelId,
                                                       @RequestParam(value = "taskId") @ApiParam(value = "发布同步列表行id") String taskId) {
        List<String> res = model2ProdSyncService.getTaskLogs(SyncToEnv.PRE_PROD, modelId, taskId);
        return ResultBean.ok(res);
    }

    @BmPermission(value = {"model:table:authorize"}, name = "发布同步-同步生产-查看日志")
    @GetMapping("/prod/log")
    @ApiOperation(value = "发布同步-同步生产-查看日志")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = Boolean.class), @ApiResponse(code = 200, message = "ok", responseContainer = "List", response = String.class)})
    public ResultBean<List<String>> syncProdViewLog(@RequestParam(value = "modelId") @ApiParam(value = "模型id") String modelId,
                                                    @RequestParam(value = "taskId") @ApiParam(value = "发布同步列表行id") String taskId) {
        List<String> res = model2ProdSyncService.getTaskLogs(SyncToEnv.PROD, modelId, taskId);
        return ResultBean.ok(res);
    }


}
